package com.googlecode.spruce.plugins.core.file;

import java.io.File;

import com.googlecode.spruce.core.DefaultPluginResult;
import com.googlecode.spruce.core.ServiceCheckPlugin;
import com.googlecode.spruce.core.ServiceCheckPluginResult;

public class FileAgeServiceCheck implements ServiceCheckPlugin
{

    private String filePath;
    private long maxAge;

    public ServiceCheckPluginResult execute()
    {
        final File file = new File(this.filePath);
        final long lastModified = file.lastModified();
        final long now = System.currentTimeMillis();
        final long delta = (now - lastModified);

        if (delta > maxAge)
        {
            return new DefaultPluginResult(0, file.getAbsolutePath() + " is older than allowable time: "
                    + this.maxAge + "(ms)");
        }

        return new DefaultPluginResult(100);
    }

    public String getFilePath()
    {
        return filePath;
    }

    public void setFilePath(final String filePath)
    {
        this.filePath = filePath;
    }

    /**
     * @return the maxAge
     */
    public long getMaxAge()
    {
        return maxAge;
    }

    /**
     * @param maxAge
     *            the maxAge to set
     */
    public void setMaxAge(final long maxAge)
    {
        this.maxAge = maxAge;
    }

}
